package org.ume.school.modules.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

/**
 * Created by Django on 2017/7/30.
 */
public class PageUtils {

    /**
     * 组成PageRequest
     *
     * @param page      ，  第一页为1 ，MySQL内部为0
     * @param limit
     * @param orderby
     * @param direction
     * @return
     */
    public static PageRequest compositePage(String page, String limit, String orderby, String direction) {
        Sort sort = null;
        if (!StringUtils.isEmpty(orderby)) {
            Sort.Direction dir = Sort.Direction.ASC;
            if (!StringUtils.isEmpty(direction)) {
                if ("DESC".equals(direction.toUpperCase())) {
                    dir = Sort.Direction.DESC;
                }
            }
            sort = new Sort(dir, orderby);
        }
        Integer offset = Integer.parseInt(page);
        if (offset <= 0) {
            offset = 1;
        }
        PageRequest pr = new PageRequest(offset - 1, Integer.parseInt(limit), sort);
        return pr;
    }
}
